/**
  ******************************************************************************
  * @file           : usart_bsp.cpp
  * @author         : Tessia
  * @brief          : None
  * @attention      : None
  * @date           : 2024/3/1
  ******************************************************************************
  */



#include "usart_bsp.h"

extern uint8_t uart_vision_rx_buf[64];
extern uint8_t uart_vision_tx_buf[64];

extern twins_receive_info twins_receive;
extern twins_receive_info_tran twins_receive_tran;

extern osSemaphoreId_t VisionUARTDataUpdateBinarySemHandle;

uint8_t huart1_err = 0;
uint32_t huart1_cnt = 0;
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size){
    if(huart->Instance == USART1){ //与左头的视觉通信
        osSemaphoreRelease(VisionUARTDataUpdateBinarySemHandle);
        huart1_cnt = 0;
        HAL_UARTEx_ReceiveToIdle_DMA(&huart1, uart_vision_rx_buf, sizeof(uart_vision_rx_buf));
        __HAL_DMA_DISABLE_IT(huart1.hdmarx, DMA_IT_HT);
        return;
    }else if(huart->Instance == USART6){ //与左头的其他信息交互
        if(twins_receive_tran.frame_header == 0x30 && twins_receive_tran.frame_tail == 0x31){
            twins_receive.is_target_detected = twins_receive_tran.is_target_detected;
            twins_receive.armor_number_last = twins_receive_tran.armor_number_last;
            twins_receive.chassis_cal_yaw = twins_receive_tran.chassis_cal_yaw;
            twins_receive.twins_yaw = twins_receive_tran.twins_yaw;
            twins_receive.twins_pitch = twins_receive_tran.twins_pitch;
            twins_receive.current_left_yaw = twins_receive_tran.current_left_yaw;
            twins_receive.is_right_of_axis = twins_receive_tran.is_right_of_axis;
            twins_receive.is_chassis_yaw_stop = twins_receive_tran.is_chassis_yaw_stop;

            HAL_UARTEx_ReceiveToIdle_DMA(&huart6, (uint8_t *)&twins_receive_tran, sizeof(twins_receive_tran));
            __HAL_DMA_DISABLE_IT(huart6.hdmarx, DMA_IT_HT);
        }else{
            HAL_UARTEx_ReceiveToIdle_DMA(&huart6, (uint8_t *)&twins_receive_tran, sizeof(twins_receive_tran));
            __HAL_DMA_DISABLE_IT(huart6.hdmarx, DMA_IT_HT);
        }
        return;
    }
}